package com.leyou.common.advice;

import com.leyou.common.exception.LyException;
import com.leyou.common.vo.ExceptionResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice  //默认自动拦截所有Controller（发送的请求）
public class CommonExceptionHandler {

	//处理异常(该方法的返回值就是返回到页面的数据)
	@ExceptionHandler(RuntimeException.class)   //设置拦截的是运行时异常
	public ResponseEntity<ExceptionResult> handleException(LyException e){
		//HttpStatus.BAD_REQUEST请求状态码.body()请求体
		return ResponseEntity.status(e.getExceptionEnum().getCode())
				.body( new ExceptionResult(e.getExceptionEnum()));
	}
}